package com.jacklei.ch15;

public class DiameterOfBinaryTree {
    public static void main(String[] args) {
        DiameterOfBinaryTree d = new DiameterOfBinaryTree();
        TreeNode treeNode = new TreeNode(1);
        treeNode.left = new TreeNode(2);
        System.out.println(d.diameterOfBinaryTree(treeNode));
    }
            int max = Integer.MIN_VALUE;
            public int diameterOfBinaryTree(TreeNode root) {
                diameterOfBinaryTree2(root);
                return max;
            }
            public int diameterOfBinaryTree2(TreeNode root) {
                if (root == null) return 0;
                int left = diameterOfBinaryTree2(root.left);
                int right = diameterOfBinaryTree2(root.right);
                int ans = (left + right);
                if(ans > max){
                    max =ans;
                }

                return Math.max(left, right)+1;

            }
    public int diameterOfBinaryTree1(TreeNode root) {
        if(root == null) return 0;
        int left = diameterOfBinaryTree1(root.left);
        int right = diameterOfBinaryTree1(root.right);
        return Math.max(left,right)+1;
    }
    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
